import { FileUploadRes } from "@/types/fileType";
import Taro from "@tarojs/taro";

// export async function uploadFile(fileData: string | Blob): Promise<FileUploadRes> {
//     const formData = new FormData();
//     formData.append('file', fileData);
//
//     return axios.post('api/v1/file/upload', formData);
// }

export async function uploadFile(filePath: string): Promise<FileUploadRes> {
  let token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NzEyMjAxNTQsInN1YiI6IjEifQ.smNKdUkZkpyH5YVc-BbrMNKZsYilQJVN2tOWW3dKH2s';

  try {
    const res = await Taro.uploadFile({
      url: process.env.TARO_APP_BASE_URL + 'api/v1/file/upload',
      filePath,
      name: 'file',
      formData: {
        timestamp: Date.now()
      },
      header: {
        Authorization: `Bearer ${token}`  // Include the token in the Authorization header
      }
    });

    if (res.statusCode === 200) {
      const result = JSON.parse(res.data);
      return result.data as FileUploadRes;
    }
    throw new Error('文件上传失败');
  } catch (error) {
    console.error('上传失败:', error);
    throw error;
  }
}

